#include "stdio.h"
//#include "stdlib.h"
#include "tanimoto.h"

#define TANIMOTO_TRESHOLD 0.93




float compare_fingerprints(int* database, int* compare_database, int database_size, int compared_db_size, tTanimotoOut *output_buffer)
{
	int fingerPrintIdx;
	int comparedFingerPrintIdx;
	int partIdx;
	int outputIdx = 0;
	float s,a,b,c;
	int resultIdx = 0;
	printf("Compare.");
	/* Finger prints from database */
	for(fingerPrintIdx = 0; fingerPrintIdx < database_size ; fingerPrintIdx++)
	{
		a = b = c = 0;
		s = 0;
		/*  Fingerprints of the other database */
		for(comparedFingerPrintIdx = 0;comparedFingerPrintIdx < compared_db_size;comparedFingerPrintIdx++)
		{
			/* Iterate thorugh fingerprints (32 integers in each)*/
			for(partIdx = 0;partIdx < 32;partIdx++)
			{
				/* Get bit counts  */
				a += count_bits(database[fingerPrintIdx + partIdx]);
				b += count_bits(compare_database[comparedFingerPrintIdx + partIdx]);
				c += count_bits(database[fingerPrintIdx + partIdx] & compare_database[comparedFingerPrintIdx + partIdx]);		
			}
			s = c/(a + b - c);
			/* Check values*/
			if(s > TANIMOTO_TRESHOLD)
			{
				output_buffer[outputIdx].idx1 = fingerPrintIdx;
				output_buffer[outputIdx].idx2  = comparedFingerPrintIdx;
				output_buffer[outputIdx].tanimoto = s;
				outputIdx++; 
			}

		}
	}
	printf("Finish.");
}

int count_bits(int v)
{
	int c = 0;
	v = v - ((v >> 1) & 0x55555555);                    // reuse input as temporary
	v = (v & 0x33333333) + ((v >> 2) & 0x33333333);     // temp
	c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
	return c;
}


